<?php

class Admin_CompanyController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
        $this->list_sess_unset('controller_company');
        $this->session_search_key = 'controller_company';
        $this->session_search_name = 'search_label';
    }

    public function indexAction() {
        $this->_helper->redirector('list', 'company', 'admin');
    }
	
	public function unsetsessionAction() {
        $pagename = $this->_getParam('pagename');
        unset($_SESSION['_LISTING_SESS']);
        $this->_helper->redirector($pagename, 'company', 'admin');
    }

    public function listAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "";
        $searchtype = "";
        $orderField = 'comp_id';
        $sort = 'asc';

        if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])) {
            $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = "";
        }
        /* Pagination Login */
        $itemsPerPageReviews = $this->PAGING_PER_PAGE;
        $currentPageReviews = 1;
		
        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }

        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();

            //Multiple Delete Logic 
            if (isset($postedData["multiaction"]) && ($postedData["multiaction"] != "")) {
                $action = trim($postedData["multiaction"]);
                if (isset($postedData["multicheck"]) && count($postedData["multicheck"]) > 0) {
                    $ids = implode(",", $_POST["multicheck"]);
                    $result = $this->deleteAction($_POST["multicheck"]);
                } else {
                    $errorMessage[] = "Please select one checkbox";
                }
            }
			/* Search Facility Over Here */
            if (isset($postedData["searchtype"]) && ($postedData["searchtype"] == "")) {
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'] = "";
            }
            if (isset($postedData["searchtype"]) && ($postedData["searchtype"] != "")) {
                $searchtype = $postedData["searchtype"];
                $where = "comp_name LIKE '%$searchtype%' OR comp_phone LIKE '%$searchtype%' OR comp_email LIKE '%$searchtype%' OR comp_type LIKE '%$searchtype%'";
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'] = $searchtype;
                $this->view->searchtype = $searchtype;
            }
        } else {

            if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'])) {
                $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'] = '';
            }
			$searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'];
            $where = "comp_name LIKE '%$searchtype%' OR comp_phone LIKE '%$searchtype%' OR comp_email LIKE '%$searchtype%' OR comp_type LIKE '%$searchtype%'";
            $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'] = $searchtype;
            $this->view->searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name]['searchtype'];
        }
        //Load company Model
        $companyModel = new Model_Company();
        $companyData = $companyModel->fetchCompanyData($where, $itemsPerPageReviews, $offset, $orderField, $sort);
        //Paginator logiq
        $paginator = new GTL_Paginator();

        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($companyModel->getCompanyCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'company', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);

        $paginator->paginate($link);
		$GTLOBJ = new GTL_Common();

        $this->view->gtlObj = $GTLOBJ;
        $this->view->paginator = $paginator;
        $this->view->companyData = $companyData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->statusArray = $companyModel->getStatusArray();
        $this->view->searchtype = $searchtype;
    }

    public function formAction() {
		
        //Initialize
        $comp_id = '';
        $successmessage = '';
        $errorMessageArray = Array();
        $data = array();
        $companyData = Array();
		$comp_available_to_everyone = "";
        $this->view->mode = 'Add';

        //Load company Model
        $companyModel = new Model_Company();
		
		//Load company Form
        $companyForm = new Form_Admin_Company();
		
		//Load client Model
        $clientModel = new Model_Client();
		
		//Load the status array for the select box in form 
        $clientArray = $clientModel->fetchClient();
        $companyForm->comp_client_id->addMultiOptions($clientArray);

        //Load the status array for the select box in form 
        $statusArray = $companyModel->getStatusArray();
        $companyForm->comp_status->addMultiOptions($statusArray);
		
		//Load the Company Currency array for the select box in form 
        $companycurrencyArray = $companyModel->getCompanycurrencyArray();
        $companyForm->comp_currency->addMultiOptions($companycurrencyArray);
		
		//Load the Company Industry array for the select box in form 
        $companyindustryArray = $companyModel->getCompanyindustryArray();
        $companyForm->comp_industry->addMultiOptions($companyindustryArray);
		
		//Load the Company Type array for the select box in form 
        $companytypeArray = $companyModel->getCompanytypeArray();
        $companyForm->comp_type->addMultiOptions($companytypeArray);
		
        //Check for the team_id Parameter
        $companyData['comp_id'] = $this->_request->getParam('comp_id');
        $comp_id = (!empty($companyData['comp_id'])) ? $companyData['comp_id'] : null;

        if (!empty($comp_id)) {

            //Set the Mode to Edit
            $this->view->mode = 'Edit';
            //Fetch data to be loaded in form 
            $data = $companyModel->fetchEntryById($comp_id); 
			$comp_available_to_everyone = $data['comp_available_to_everyone'];
        }

        //Populate form 
        $this->view->form = $companyForm->populate($data);
		
        if ($this->getRequest()->isPost()) {
            //Check for Errors
            $postedData = $this->_request->getPost();
            if (!$companyForm->isValid($postedData)) {
                $errorMessage = $companyForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {
               
                if (count($errorMessageArray) > 0) {
                    
                } else {

					if ($this->view->mode == 'Add') {
                        $companyData['comp_createddate'] = date('Y-m-d H:i:s');
						$companyData['comp_addedby'] = $this->USER->lgn_id;
                    }
                    //vat save
                    $companyData['comp_id'] = $comp_id;
                    $companyData['comp_client_id'] = $postedData['comp_client_id'];
                    $companyData['comp_name'] = $postedData['comp_name'];
					$companyData['comp_responsible_person'] = $this->USER->lgn_id;
					$companyData['comp_type'] = $postedData['comp_type'];
					$companyData['comp_industry'] = $postedData['comp_industry'];
                    $companyData['comp_currency'] = $postedData['comp_currency'];
					$companyData['comp_comment'] = $postedData['comp_comment'];
					if (array_key_exists('comp_available_to_everyone', $postedData)) {
						$companyData['comp_available_to_everyone'] = $postedData['comp_available_to_everyone'];
					} else {
						$companyData['comp_available_to_everyone'] = 0;
					}
					$companyData['comp_email'] = $postedData['comp_email'];
					$companyData['comp_phone'] = $postedData['comp_phone'];
                    $companyData['comp_site'] = $postedData['comp_site'];
					$companyData['comp_adrline1'] = $postedData['comp_adrline1'];
					$companyData['comp_adrline2'] = $postedData['comp_adrline2'];
                    $companyData['comp_adrline3'] = $postedData['comp_adrline3'];
					$companyData['comp_street'] = $postedData['comp_street'];
					$companyData['comp_postal'] = $postedData['comp_postal'];
                    $companyData['comp_city'] = $postedData['comp_city'];
					$companyData['comp_country'] = $postedData['comp_country'];
					$companyData['comp_payment_details'] = $postedData['comp_payment_details'];
					$companyData['comp_status'] = $postedData['comp_status'];
					$companyData['comp_updatedby'] = $this->USER->lgn_id;
                    $companyData['comp_updateddate'] = date('Y-m-d H:i:s');
					//echo '<pre>';print_r($postedData);exit;
                    $companyModel->save($companyData, 'comp_id');
                    if ($this->view->mode == 'Add') {
                        $successmessage = 'Company add successfully';
                    } else {
                        $successmessage = 'Company updated successfully';
                    }
                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'company', 'admin');
                }
            }
        }
        $this->view->compid = $comp_id;
		$this->view->comp_available_to_everyone = $comp_available_to_everyone;
		$this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $companyForm;
    }

    public function deleteAction($ids = Array()) {
		
		//Load company Model
        $companyModel = new Model_Company();
		
        if ($this->_getParam('comp_id') && $this->_getParam('comp_id') != '') {
            $comp_id = $this->_getParam('comp_id');

            $companyData = array();
            $companyData = $companyModel->fetchEntryById($comp_id);
            
            $companyModel->deleteCompany($comp_id);
            $this->addSuccessMessage('Company Successfully Deleted');
            $this->_helper->redirector('list', 'company', 'admin');
        } else {
            $companyModel->deleteCompany($ids);
            $this->addSuccessMessage('Company Successfully Deleted');
            $this->_helper->redirector('list', 'company', 'admin');
        }
    }

}

?>